<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka选举机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #4f46e5;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 卡片悬停效果 */
        .card-hover {
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        
        /* 渐变文字效果 */
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        /* 数字标记样式 */
        .number-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 2rem;
            height: 2rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 700;
            margin-right: 0.75rem;
            flex-shrink: 0;
        }
        
        /* 引用样式 */
        .quote-box {
            position: relative;
            padding-left: 3rem;
        }
        
        .quote-box::before {
            content: '"';
            position: absolute;
            left: 0;
            top: -1rem;
            font-size: 4rem;
            color: #e0e7ff;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 图片容器样式 */
        .image-container {
            position: relative;
            overflow: hidden;
            border-radius: 1rem;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }
        
        .image-container img {
            transition: transform 0.3s ease;
        }
        
        .image-container:hover img {
            transform: scale(1.05);
        }
        
        /* 时间线样式 */
        .timeline-item {
            position: relative;
            padding-left: 3rem;
            padding-bottom: 2rem;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 0.75rem;
            top: 0.5rem;
            bottom: -2rem;
            width: 2px;
            background: #e0e7ff;
        }
        
        .timeline-item:last-child::before {
            bottom: 0;
        }
        
        .timeline-dot {
            position: absolute;
            left: 0;
            top: 0.5rem;
            width: 1.5rem;
            height: 1.5rem;
            background: white;
            border: 3px solid #667eea;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-500 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24 text-center">
            <h1 class="text-5xl md:text-7xl font-bold mb-6 animate-pulse">
                <i class="fas fa-network-wired mr-4"></i>
                Kafka选举机制
            </h1>
            <p class="text-xl md:text-2xl mb-8 font-light">深入解析分布式系统的核心：Leader选举与Controller机制</p>
            <div class="flex justify-center space-x-4">
                <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                    <i class="fas fa-server mr-2"></i>
                    Broker选举
                </span>
                <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                    <i class="fas fa-crown mr-2"></i>
                    Controller选举
                </span>
                <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full">
                    <i class="fas fa-sync-alt mr-2"></i>
                    高可用保障
                </span>
            </div>
        </div>
    </section>

    <!-- 主要内容区 -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- 引言卡片 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="quote-box">
                <p class="text-lg text-gray-700 leading-relaxed drop-cap">
                    在Kafka中，主要涉及两个关键的选举机制：Broker的Leader选举和Controller的选举。这两个机制保障了Kafka集群在面对节点故障、网络问题等情况时能够迅速、可靠地进行Leader的选择，确保数据的一致性和可用性。
                </p>
            </div>
        </div>

        <!-- 概念关系图 -->
        <div class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-2xl p-8 mb-12">
            <h3 class="text-2xl font-bold mb-6 text-center gradient-text">
                <i class="fas fa-project-diagram mr-2"></i>
                Kafka选举机制架构图
            </h3>
            <div class="mermaid">
                graph TB
                    A[Kafka集群] --> B[Broker Leader选举]
                    A --> C[Controller选举]
                    B --> D[Leader失效检测]
                    B --> E[Follower选举请求]
                    B --> F[ISR更新]
                    C --> G[Controller失效检测]
                    C --> H[ZooKeeper协调]
                    C --> I[顺序节点机制]
                    H --> J[分布式锁]
                    H --> K[强一致性保证]
                    style A fill:#f9f,stroke:#333,stroke-width:4px
                    style B fill:#bbf,stroke:#333,stroke-width:2px
                    style C fill:#bbf,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Broker Leader选举部分 -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 flex items-center">
                <span class="gradient-text">
                    <i class="fas fa-server mr-3"></i>
                    1、Broker的Leader选举
                </span>
            </h2>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8">
                <p class="text-gray-700 leading-relaxed mb-6 text-lg">
                    在Kafka中，Broker的Leader选举过程是关键的分布式系统设计，确保在节点故障或网络问题时，能够快速而可靠地选举新的Leader。这个过程主要依赖于ZooKeeper协调服务。
                </p>
                
                <!-- 选举流程时间线 -->
                <div class="mt-8">
                    <h3 class="text-2xl font-semibold mb-6 text-gray-800">
                        <i class="fas fa-timeline mr-2 text-purple-600"></i>
                        选举流程步骤
                    </h3>
                    
                    <div class="space-y-6">
                        <div class="timeline-item">
                            <div class="timeline-dot"></div>
                            <div class="bg-gradient-to-r from-blue-50 to-indigo-50 rounded-xl p-6 card-hover">
                                <h4 class="text-xl font-semibold mb-2 flex items-center">
                                    <span class="number-badge">1</span>
                                    Leader失效检测
                                </h4>
                                <p class="text-gray-700 leading-relaxed">
                                    每个Broker都会定期检测与其他Broker通信的状态，包括Leader和Follower之间的心跳检测。如果一个Broker检测到Leader不可用，它会发起Leader选举。
                                </p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <div class="timeline-dot"></div>
                            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6 card-hover">
                                <h4 class="text-xl font-semibold mb-2 flex items-center">
                                    <span class="number-badge">2</span>
                                    Follower提出选举请求
                                </h4>
                                <p class="text-gray-700 leading-relaxed">
                                    当Follower检测到Leader不可用时，它会提出选举请求。这个请求包含了Follower的元数据信息，例如副本信息、日志的最新偏移量等。
                                </p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <div class="timeline-dot"></div>
                            <div class="bg-gradient-to-r from-green-50 to-teal-50 rounded-xl p-6 card-hover">
                                <h4 class="text-xl font-semibold mb-2 flex items-center">
                                    <span class="number-badge">3</span>
                                    选举请求的传播
                                </h4>
                                <p class="text-gray-700 leading-relaxed">
                                    选举请求会通过ZooKeeper在集群中传播。ZooKeeper作为分布式协调服务，维护了一个Controller节点，存储当前的Controller的信息。
                                </p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <div class="timeline-dot"></div>
                            <div class="bg-gradient-to-r from-yellow-50 to-orange-50 rounded-xl p-6 card-hover">
                                <h4 class="text-xl font-semibold mb-2 flex items-center">
                                    <span class="number-badge">4</span>
                                    Leader的选择
                                </h4>
                                <p class="text-gray-700 leading-relaxed">
                                    Controller协调各个Broker，根据提出选举请求的Follower的元数据信息来选择新的Leader。通常选择具有最新日志并且网络延迟最小的Follower作为新的Leader。
                                </p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <div class="timeline-dot"></div>
                            <div class="bg-gradient-to-r from-red-50 to-pink-50 rounded-xl p-6 card-hover">
                                <h4 class="text-xl font-semibold mb-2 flex items-center">
                                    <span class="number-badge">5</span>
                                    ISR的更新
                                </h4>
                                <p class="text-gray-700 leading-relaxed">
                                    新的Leader选出后，Controller会更新ISR列表。ISR是一组与Leader保持同步状态的副本，确保在Leader失效时，仍然有足够的副本具备最新的数据。
                                </p>
                            </div>
                        </div>
                        
                        <div class="timeline-item">
                            <div class="timeline-dot"></div>
                            <div class="bg-gradient-to-r from-indigo-50 to-blue-50 rounded-xl p-6 card-hover">
                                <h4 class="text-xl font-semibold mb-2 flex items-center">
                                    <span class="number-badge">6</span>
                                    ZooKeeper在Leader选举中的作用
                                </h4>
                                <p class="text-gray-700 leading-relaxed">
                                    它通过提供分布式锁和协调服务，确保了在集群中只有一个Controller，并且协调了Broker的Leader选举。ZooKeeper的强一致性和持久性特性保障了选举的可靠性。
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Controller选举部分 -->
        <section class="mb-16">
            <h2 class="text-4xl font-bold mb-8 flex items-center">
                <span class="gradient-text">
                    <i class="fas fa-crown mr-3"></i>
                    2、Controller选举
                </span>
            </h2>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8">
                <p